package Servlet;

import Utils.DbUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String tableName = req.getParameter("tableName");
        String[] columns = req.getParameterValues("column");

        Map<String,String> columnTypes = DbUtil.getColumnAndType(req, tableName);

        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("INSERT INTO ").append(tableName).append(" VALUES (");
        int count = 0;
        List<Object> params = new ArrayList<>();
        try {
            for (String column : columns) {
                String type = columnTypes.get(column);
                Object value = null;

                String paramValue = req.getParameter(column);
                if (!paramValue.isEmpty()) {
                    switch (type) {
                        case "INT":
                            value = Integer.parseInt(paramValue);
                            break;
                        case "VARCHAR":
                        case "CHAR":
                            value = paramValue;
                            break;
                        case "DOUBLE":
                            value = Double.parseDouble(paramValue);
                            break;
                        case "FLOAT":
                            value = Float.parseFloat(paramValue);
                            break;
                        default:
                            try {
                                throw new Exception("该表暂时不支持数据类型增加");
                            } catch (Exception e) {
                                req.setAttribute("error", e.getMessage());
                                RequestDispatcher rd = req.getRequestDispatcher("error.jsp");
                                rd.forward(req, resp);
                            }
                    }
                }
                if (value == null) {
                    sqlBuilder.append("NULL");
                } else {
                    sqlBuilder.append("?");
                    params.add(value);
                }
                if (++count < columns.length) {
                    sqlBuilder.append(",");
                }
            }
            sqlBuilder.append(")");
            String sql = sqlBuilder.toString();
            System.out.println(sql);
            System.out.println(params.toString());
            DbUtil.executeUpdate(req,resp,sql,params.toArray());
            resp.sendRedirect("GetTableServlet?tableName="+tableName);
        }catch (NumberFormatException e){
            req.setAttribute("error", e.getMessage());
            RequestDispatcher rd = req.getRequestDispatcher("error.jsp");
            rd.forward(req, resp);
        }

    }
}
